// miniprogram/pages/select_car/index.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cateItems: [],
    curNav: 1,
    curIndex: 0
  },

  login: function() {
    //获取产品信息
    let that = this;
    wx.request({
      url: app.globalData.hostname + ":" + app.globalData.port + '/select_car/product',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        car_type: "0"
      },
      success: function(res) {
        for (var i = 0; i < res.data.product_list.length; i++) {
          var array = wx.base64ToArrayBuffer(res.data.product_list[i].image);
          var base64 = wx.arrayBufferToBase64(array);
          res.data.product_list[i].image = "data:image/png;base64," + base64.substr(1);
        }
        that.setData({
          cateItems: [{
              cate_id: 1,
              cate_name: "全部",
              ishaveChild: true,
              children: res.data.product_list
            },
            {
              cate_id: 2,
              cate_name: "主婚车车型",
              ishaveChild: true,
              children: []
            },
            {
              cate_id: 3,
              cate_name: "个性车型",
              ishaveChild: true,
              children: [],
            },
          ]
        })
      }
    })
  },
  onShow: function() {
    this.login()
  },

  //事件处理函数  
  switchRightTab: function(e) {
    // 获取item项的id，和数组的下标值  
    let that = this;
    let id = e.target.dataset.id,
      index = parseInt(e.target.dataset.index);
    wx.request({
      url: app.globalData.hostname + ":" + app.globalData.port + '/select_car/product',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        car_type: index
      },
      success: function(res) {
        for (var i = 0; i < res.data.product_list.length; i++) {
          var array = wx.base64ToArrayBuffer(res.data.product_list[i].image);
          var base64 = wx.arrayBufferToBase64(array);
          res.data.product_list[i].image = "data:image/png;base64," + base64.substr(1);
        }
        if (index == '1') {
          that.setData({
            cateItems: [{
                cate_id: 1,
                cate_name: "全部",
                ishaveChild: true,
                children: []
              },
              {
                cate_id: 2,
                cate_name: "主婚车车型",
                ishaveChild: true,
                children: res.data.product_list
              },
              {
                cate_id: 3,
                cate_name: "个性车型",
                ishaveChild: true,
                children: [],
              },
            ]
          })
        }
        if (index == '0') {
          that.setData({
            cateItems: [{
                cate_id: 1,
                cate_name: "全部",
                ishaveChild: true,
                children: res.data.product_list
              },
              {
                cate_id: 2,
                cate_name: "主婚车车型",
                ishaveChild: true,
                children: []
              },
              {
                cate_id: 3,
                cate_name: "个性车型",
                ishaveChild: true,
                children: [],
              },
            ]
          })
        }
        if (index == '2') {
          that.setData({
            cateItems: [{
                cate_id: 1,
                cate_name: "全部",
                ishaveChild: true,
                children: []
              },
              {
                cate_id: 2,
                cate_name: "主婚车车型",
                ishaveChild: true,
                children: []
              },
              {
                cate_id: 3,
                cate_name: "个性车型",
                ishaveChild: true,
                children: res.data.product_list
              },
            ]
          })
        }

      }
    })
    // 把点击到的某一项，设为当前index  
    this.setData({
      curNav: id,
      curIndex: index
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})